<?php
/**
 * Created by PhpStorm.
 * User: zhao
 * Date: 2018/6/3
 * Time: 16:45
 */

namespace App\Obersvers;


use App\Models\BalanceRecord;
use App\Models\CreditsRecod;
use App\Models\MerchantTicket;
use App\Models\ShareGiftRecord;

class ShareGiftRecordObersver
{
    public function created(ShareGiftRecord $record)
    {

        $share_rule = json_decode($record->old_concession_share_data,true);

        switch ($share_rule['type']){
            case 'register':

                $this->_shareRegister($record,$share_rule);

                break;
        }

        /*if($record->cash){
            $insert_data['value'] = $record->cash;

            BalanceRecord::create($insert_data);
        }*/
    }

    protected function _shareRegister($record,$share_rule)
    {

        $insert_data['id_merchant'] = $record->id_merchant;
        $insert_data['table_type'] = 'share_gift_record';
        $insert_data['table_id'] = $record->id;

        $insert_data['desc'] = $share_rule['action'];

        $time = now()->toDateTimeString();
        $insert_data['created_at'] = $time;
        $insert_data['updated_at'] = $time;

        if($share_rule['reward_credits']){
            $insert_data['id_wxuser_mcard'] = $record->id_share_wxuser_mcard;
            $insert_data['value'] = $share_rule['reward_credits'];
            $insert_data['title'] = '新会员通过您分享注册';
            CreditsRecod::create($insert_data);
        }

        if($share_rule['reward_credits_other']){
            $insert_data['id_wxuser_mcard'] = $record->id_currenct_wxuser_mcard;
            $insert_data['value'] = $share_rule['reward_credits_other'];
            $insert_data['title'] = '分享注册奖励';
            CreditsRecod::create($insert_data);
        }
    }
}